package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.misc.ExtensionInstallationException;

import java.util.List;
import java.util.Objects;

/**
* @author MrWang
* @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
* @createDate 2023-10-25 16:14:05
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    //添加购物车
    @Transactional
    @Override
    public Result saveShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = BeanCopyUtils.copyBean(shoppingCartDTO, ShoppingCart.class);
        Long dishId = shoppingCart.getDishId();
        String dishFlavor = shoppingCart.getDishFlavor();
        Long setmealId = shoppingCart.getSetmealId();
        //0.先查看该购物车是否已经存在 已经存在就将数量加一即可
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .eq(Objects.nonNull(dishId),ShoppingCart::getDishId,dishId)
                .eq(StringUtils.hasText(dishFlavor),ShoppingCart::getDishFlavor,dishFlavor)
                .eq(Objects.nonNull(setmealId),ShoppingCart::getSetmealId,setmealId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        //注意:这里只可能有两种情况 shoppingCart1要么为空 要么只有一条数据！！！
        //已经存在 数量加一即可
        if (shoppingCarts != null && shoppingCarts.size() > 0){
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);//获取列表第一个
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateById(shoppingCart1);
        } else {
            //给购物车部分属性赋值
            shoppingCart.setUserId(BaseContext.getCurrentId());
            if (setmealId != null){
                //1.套餐类型
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            } else {
                //2.菜品类型
                if (dishId == null){
                    return Result.error("购物车不能为空");
                } else {
                    if (StringUtils.hasText(dishFlavor)){//口味赋值
                        shoppingCart.setDishFlavor(dishFlavor);
                    }
                    Dish dish = dishMapper.selectById(dishId);
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setImage(dish.getImage());
                    shoppingCart.setAmount(dish.getPrice());
                }
            }
            shoppingCartMapper.insert(shoppingCart);
        }

        return Result.success();
    }

    //查看购物车列表
    @Override
    public Result shoppingCartList() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);

        return Result.success(shoppingCarts);
    }

    //删除购物车中一个商品
    @Transactional
    @Override
    public Result deleteOneShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .eq(Objects.nonNull(shoppingCartDTO.getDishId()),ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                .eq(StringUtils.hasText(shoppingCartDTO.getDishFlavor()),ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor())
                .eq(Objects.nonNull(shoppingCartDTO.getSetmealId()),ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId());
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
        if (shoppingCart.getNumber() == 1){ //查询数量只有一个就删除
            shoppingCartMapper.delete(queryWrapper);
        } else {// 数量减一即可
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateById(shoppingCart);
        }

        return Result.success();
    }

    //清空购物车
    @Override
    public Result cleanAllShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartMapper.delete(queryWrapper);

        return Result.success();
    }

}




